#include "Progress.h"
#include "../debug_new.h"
#include "FAlpProgress.h"



CFAlpProgress::CFAlpProgress()
{
	Init();
}

CFAlpProgress::~CFAlpProgress()
{

}

CFAlpProgress::CFAlpProgress(UINT nID):CBaseICtrl(nID)
{
	Init();
}

CFAlpProgress::CFAlpProgress(wstring strCaption,UINT nID)
:	CBaseICtrl(strCaption,nID)
{
	Init();
}

CFAlpProgress::CFAlpProgress(DWORD dwStyle,wstring strCaption, const CRect &rc, UINT nID)
:	CBaseICtrl(dwStyle,strCaption, rc, nID)
{
	Init();
}

CFAlpProgress::CFAlpProgress(DWORD dwStyle, wstring strCaption, int x, int y, int cx, int cy, UINT nID)
:	CBaseICtrl(dwStyle, strCaption, x, y, cx, cy, nID)
{
	Init();
}

void CFAlpProgress::Init()
{
	m_nPos = m_nMin = 0;
	m_nMax = 100;
	m_nPage = 20;

	m_nMarginLeft = m_nMarginRight = m_nMarginTop = m_nMarginBottom = 0;
	m_nThumbWidth = m_nThumbHeight = 0;

	m_bChannel = m_bVertical = m_bThumb  = FALSE;


	m_pAlpResChnl = NULL;
	m_pAlpResChnAct = NULL;

	m_pAlpResThumb = NULL;
	m_pAlpResActThumb = NULL;
}

// Sets the maximum range for the slider.
//
// Parameters:
//		[IN]	nMax
//				Maximum position for the slider.
//		[IN]	bRedraw
//				TRUE to redraw after the range is set.
//				FALSE to only change maximum position.
//
void CFAlpProgress::SetRangeMax(int nMax, BOOL bRedraw)
{
	m_nMax = nMax;
	/*if( bRedraw )
		Invalidate();*/
}

// Sets the minimum range for the slider.
//
// Parameters:
//		[IN]	nMin
//				Minimum position for the slider.
//		[IN]	bRedraw
//				TRUE to redraw after the range is set.
//				FALSE to only change minimum position.
//
void CFAlpProgress::SetRangeMin(int nMin, BOOL bRedraw)
{
	m_nMin = nMin;
	/*if( bRedraw )
		Invalidate();*/
}

// Sets the range (minimum and maximum positions) for the slider.
//
// Parameters:
//		[IN]	nMin
//				Minimum position for the slider.
//		[IN]	nMax
//				Maximum position for the slider.
//		[IN]	bRedraw
//				TRUE to redraw after the range is set.
//				FALSE to only change the range.
//
void CFAlpProgress::SetRange(int nMin, int nMax, BOOL bRedraw)
{
	SetRangeMin( nMin, FALSE );
	SetRangeMax( nMax, bRedraw );
}

// Sets the current position of the slider.
//
// Parameters:
//		[IN]	nPos
//				Specifies the new slider position.
//
void CFAlpProgress::SetPos(int nPos)
{
	m_nPos = nPos;

	// Boundary check
	if( m_nPos >= m_nMax )
	{
		m_nPos = m_nMax;
	}
	if( m_nPos <= m_nMin )
	{
		m_nPos = m_nMin;
	}

	//Invalidate();
	//UpdateWindow();
}

// Sets the size of the page for a control.
//
// Parameters:
//		[IN]	nSize
//				The new page size of the control.
//
// Return value:
//		The previous page size.
//
int CFAlpProgress::SetPageSize(int nSize)
{
	int nRet = m_nPage;

	m_nPage = nSize;

	return nRet;
}

// Sets the left, top, right, and bottom margins for a control
//
void CFAlpProgress::SetMargin(int nLeft, int nTop, int nRight, int nBottom )
{
	SetMarginLeft( nLeft );
	SetMarginTop( nTop );
	SetMarginRight( nRight );
	SetMarginBottom( nBottom );
}


// Specify whether draw focus rectangle or not.
//
//		[IN]	bDraw
//				TRUE to draw focus rectangle.
//				FALSE to hide focus rectangle.
//
//		[IN]	bRedraw
//				TRUE to redraw status is changed.
//				FALSE to only change the status.
//
void CFAlpProgress::DrawFocusRect(BOOL bDraw, BOOL bRedraw)
{

}

BOOL	CFAlpProgress::SetChannelRes( wstring strResChnl,wstring strResActChnl, CMyUnCompress* pPack )
{
	BOOL br = TRUE;
	if ( strResChnl.length()==0 )
		return FALSE;
	
	if ( m_pAlpResChnl==NULL )	
		m_pAlpResChnl = new CFAlphaRes();
	br = br&&m_pAlpResChnl->LoadResData( strResChnl,pPack );

	if ( _wcsicmp(strResChnl.c_str(),strResActChnl.c_str())==0 ){
		if ( m_pAlpResChnAct!=NULL ){	
			delete m_pAlpResChnAct;
			m_pAlpResChnAct = NULL;
		}
		m_pAlpResChnAct = new CFAlphaRes( *(m_pAlpResChnl) );
	}
	else
	{
		if ( m_pAlpResChnAct==NULL )	
			m_pAlpResChnAct = new CFAlphaRes();
		br = br&&m_pAlpResChnAct->LoadResData( strResChnl,pPack );
	}
	m_bChannelActive = br;
	return br;

}

BOOL	CFAlpProgress::SetThumbRes( wstring strResThumb,wstring strResActThumb, CMyUnCompress* pPack )
{
	BOOL br = TRUE;
	if ( strResThumb.length()==0 )
		return FALSE;

	if ( m_pAlpResThumb==NULL )	
		m_pAlpResThumb = new CFAlphaRes();
	br = br&&m_pAlpResThumb->LoadResData( strResThumb,pPack );

	if ( _wcsicmp(strResThumb.c_str(),strResActThumb.c_str())==0 ){
		if ( m_pAlpResActThumb!=NULL ){	
			delete m_pAlpResActThumb;
			m_pAlpResActThumb = NULL;
		}
		m_pAlpResActThumb = new CFAlphaRes( *(m_pAlpResChnl) );
	}
	else
	{
		if ( m_pAlpResActThumb==NULL )	
			m_pAlpResActThumb = new CFAlphaRes();
		br = br&&m_pAlpResActThumb->LoadResData( strResActThumb,pPack );
	}
	
	m_bThumb = br;
	
	return br;
}


// Calculate point of thumb from position value
//
int CFAlpProgress::Pos2Pixel(int nPos)
{
	if( m_bVertical )
	{
		return
			m_nMarginTop + m_nThumbHeight/2 +
			(int)(
			( m_nHeight - m_nMarginTop - m_nMarginBottom - m_nThumbHeight ) *
			((double) ( nPos - m_nMin ) / ( m_nMax - m_nMin ) )
			);

	} else
	{
		return (int)(
			( m_nWidth - m_nMarginLeft - m_nMarginRight - m_nThumbWidth ) *
			((double) ( nPos - m_nMin ) / ( m_nMax - m_nMin ) )
			) + m_nMarginLeft + m_nThumbWidth/2;
	}
}

// Calculate position value from point of mouse
//
int CFAlpProgress::Pixel2Pos(int nPixel)
{
	if( m_bVertical )
	{
		return (int)(
		  m_nMin +
			(double)( nPixel - m_nMarginTop - m_nThumbHeight/2) /
			( m_nHeight - m_nMarginBottom - m_nMarginTop - m_nThumbHeight ) *
			( m_nMax - m_nMin )
			);
	}
	else
	{
		return (int)(
			m_nMin +
			(double)( nPixel - m_nMarginLeft - m_nThumbWidth/2 ) /
			( m_nWidth - m_nMarginLeft - m_nMarginRight - m_nThumbWidth ) *
			( m_nMax - m_nMin )
			);
	}
}



void  CFAlpProgress::ImprovePaint( HANDLE hDB ,HDC hdc  )
{
	if(m_rcControl.IsRectEmpty())
		return ;
	if(!IsVisible())
		return;
	
	// Draw channel
	if( m_bChannel ) 
	{
		// There is a bitmap for active channel
		// Lower part
		if( m_pAlpResChnl )
		{
			FASTBLTALPHABPP( m_pAlpResChnl, hDB,m_rcControl.TopLeft(),GetConBKSize() );
		}

		if( m_bChannelActive) 
		{
			// Vertical slider
			if( m_bVertical ) {
				// Upper part
				CRect rcPart( 0,0,m_rcControl.Width(),Pos2Pixel(m_nPos) );
				if( m_pAlpResChnAct )
					FASTBLTALPHABPP( m_pAlpResChnAct, hDB,m_rcControl.TopLeft(), GetConBKSize() );
			}
			else // Horizontal slider
			{
				CRect rcPart( 0,0,Pos2Pixel(m_nPos),m_rcControl.Height() );
				if( m_pAlpResChnAct )
					FASTBLTALPHABPP( m_pAlpResChnAct, hDB,m_rcControl.TopLeft(), GetConBKSize() );
			}
		}
	}

	// Draw thumb
	if( m_bThumb )
	{
		if( m_bThumbActive && (m_nState & STATE_PUSHED))
		{	
			CPoint pt( m_rcControl.left+m_nMarginLeft, m_rcControl.top+Pos2Pixel(m_nPos) - m_nThumbHeight/2 );
			FASTBLTALPHABPP( m_pAlpResActThumb, hDB,pt,GetConBKSize() );
		}
		else
		{
			CPoint pt( m_rcControl.left+Pos2Pixel(m_nPos) - m_nThumbWidth/2, m_rcControl.top+m_nMarginTop );
			FASTBLTALPHABPP( m_pAlpResThumb,  hDB,pt,GetConBKSize() );
		}

	} // if draw thumb

}

void CFAlpProgress::Paint(CDC* pDC)
{
	return;
}

void CFAlpProgress::Click(DWORD dwClick, POINT point)
{
	BOOL bSendMsg = FALSE;
	if(!IsEnabled())
	{
		return;
	}

	if(!m_bVisibale)
	{
		return;
	}

	if(!m_pContainer || !m_pContainer->m_hWnd)
	{
		return;
	}

	if(!m_rcControl.PtInRect(point))
	{ 
		m_nState = STATE_NORMAL;
		return;
	}
	else
	{
		if(dwClick & CLICK_DOWN)
		{			
			m_nState = STATE_PUSHED;
			bSendMsg = TRUE;
		}
		else if(dwClick & CLICK_UP)
		{
			if(m_pContainer->m_hWnd)
				//PostMessage(m_pContainer->m_hWnd,WM_COMMAND, m_nID,NULL);
				bSendMsg = TRUE;
			m_nState = STATE_NORMAL;
		}
	}

	point.x -= m_rcControl.left;
	point.y -= m_rcControl.top;

/////////////////////////////////////////////////////

	if( m_bVertical ) 
	{
		if( abs( point.y - Pos2Pixel( m_nPos ) ) <= m_nThumbHeight / 2 )
			m_nMouseOffset = point.y - Pos2Pixel( m_nPos );
		else
			m_nMouseOffset = 0;
	}
	else
	{
		if( abs( point.x - Pos2Pixel( m_nPos ) ) <= m_nThumbWidth / 2 )
			m_nMouseOffset = point.x - Pos2Pixel( m_nPos );
		else
			m_nMouseOffset = 0;
	}

/////////////////////////////////////

	int nPixel;

	// Boundary check
	if( m_bVertical )
	{
		nPixel = point.y - m_nMouseOffset;

		if( nPixel > m_nHeight - m_nMarginBottom - m_nThumbHeight/2 )
			nPixel = m_nHeight - m_nMarginBottom - m_nThumbHeight/2;

		if( nPixel < m_nMarginTop + m_nThumbHeight/2 )
			nPixel = m_nMarginTop + m_nThumbHeight/2;

	} 
	else
	{
		nPixel = point.x - m_nMouseOffset;

		if( nPixel < m_nMarginLeft + m_nThumbWidth/2 )
			nPixel = m_nMarginLeft + m_nThumbWidth/2;

		if( nPixel > m_nWidth - m_nMarginRight - m_nThumbWidth/2 )
			nPixel = m_nWidth - m_nMarginRight - m_nThumbWidth/2;
	}

	// Apply change
	if( Pos2Pixel(m_nPos) != nPixel )
	{
		SetPos( Pixel2Pos( nPixel ) );
	}

	if(bSendMsg)
	{
		PostMessage(m_pContainer->m_hWnd,WM_COMMAND, m_nID,NULL);
	}
}